package com.keshihao.eduservice.controller;


import com.keshihao.commonutils.R;
import com.keshihao.eduservice.entity.EduChapter;
import com.keshihao.eduservice.entity.chapter.ChapterVo;
import com.keshihao.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author keshihao
 * @since 2020-05-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    // 课程大纲列表,根据课程id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);

        return R.ok().data("allChapterVideo",list);

    }

    // 添加章节
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
      eduChapterService.save(eduChapter);
      return R.ok();
    }

    // 根据章节id查询章节
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
      EduChapter eduChapter = eduChapterService.getById(chapterId);
      return R.ok().data("eduChapter",eduChapter);
    }

    // 修改章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
      eduChapterService.updateById(eduChapter);
      return R.ok();
    }

    // 删除章节
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {

      Boolean flag = eduChapterService.deleteChapter(chapterId);

      if (flag) {
        return R.ok();
      }

      return R.error();
    }

}

